<?php
/**
 * Created by PhpStorm.
 * User: ouryun
 * Date: 2021/5/11
 * Time: 15:43
 */

namespace app\admin\controller;


use think\admin\Controller;


/**
 * 数据字典
 * Class Dictionaries
 * @package app\admin\controller
 */
class Dictionaries extends Controller
{
    /**
     * 数据字典表
     * @var string
     */
    protected $table = 'lg_data_dictionaries';
    protected $datas;

    /**
     * 数据字典列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @auth true
     * @login true
     */
    public function index()
    {
        $this->title = '数据字典';
        $query = $this->_query($this->table);
        $this->type = input('type', 'face_data');
        if ($this->type === 'face_data') {
            $query->where(['type' => 'face_data']);
        } elseif ($this->type === 'device_status') {
            $query->where(['type' => 'device_status']);
        }
        $query->page();
    }

    /**
     * 添加
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @auth true
     * @login true
     */
    public function add()
    {
        $type = input('type', 'face_data');
        if ($this->request->isPost()) {
            $data = [
                'type' => $type,
                'field' => input('field'),
                'explain' => input('explain'),
                'create_time' => time()
            ];
            $result = $this->app->db->name($this->table)->insert($data);
            if (!$result) {
                $this->error('添加失败');
            }
            sysoplog('数据字典', '添加数据成功');
            $this->success('添加成功');
        }
        $this->_form($this->table, 'form');
    }

    /**
     * 修改
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @auth true
     * @login true
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = [
                'field' => input('field'),
                'explain' => input('explain'),
                'update_time' => time()
            ];
            $result = $this->app->db->name($this->table)->where(['id' => input('id')])->update($data);
            if (!$result) {
                $this->error('失败');
            }
            sysoplog('数据字典', '修改id为' . $result);
            $this->success('成功');
        }
        $this->_form($this->table, 'form');
    }

    /**
     * 删除
     * @throws \think\db\exception\DbException
     * @auth true
     * @login true
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

    /**
     * 删除结果处理
     * @param boolean $result
     */
    protected function _remove_delete_result($result)
    {
        if ($result) {
            $map = $this->_vali(['auth.require#id' => 'ID不能为空！']);
            sysoplog('数据字典', "删除数据字典id为：{$map['auth']}");
            $this->success("删除成功！");
        } else {
            $this->error("删除失败");
        }
    }
}